
package samples;

// {{{ imports
import flash.system.LoaderContext;
import flash.system.ApplicationDomain;
import flash.display.Loader;
import flash.display.Sprite;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.ui.Keyboard;
import jp.ize.ux.UXRoot;
import jp.ize.ux.UXFocusManager;
import jp.ize.ux.components.UXLine;
import jp.ize.ux.components.UXButton;
import jp.ize.ux.components.UXLabel;
import jp.ize.ux.components.UXSelector;
import jp.ize.ux.components.UXItem;
import jp.ize.ux.components.UXImage;
import jp.ize.ux.components.UXWindow;
import jp.ize.ux.components.UXTextField;
import jp.ize.ux.components.UXProgressBar;
import jp.ize.ux.components.UXScrollBar;
import jp.ize.ux.components.UXScrollArea;
import jp.ize.ux.components.UXSlider;
import jp.ize.ux.components.UXComponentResourceFactory;
import jp.ize.ux.interactions.UXKeyInteraction;
import jp.ize.ux.acts.Act;
import jp.ize.ux.events.UXMouseEvent;
import jp.ize.ux.events.UXKeyboardEvent;
import jp.ize.ux.tween.easings.Linear;
import jp.ize.ux.tween.easings.Expo;
import jp.ize.ux.geom.UXPoint;
import jp.ize.ux.geom.UXSize;
import jp.ize.iwp.RuntimeEngine;
import jp.ize.iwp.utils.DebugUtil;
// }}}

class Components
{
	// {{{ properties

	public var reverse  : Bool;
	public var count    : Int;
	public var total    : Int;
	public var progress : UXProgressBar;

	// }}}
	// {{{ public static function main() : Void

	public static function main() : Void
	{
		RuntimeEngine.init();
		DebugUtil.init();
		DebugUtil.setLocation( 300, 300 );
		/*
		var comp = new UXLine( 1.0, 0xFFFFFF, new UXPoint(0,0), new UXPoint(200,200) );
		*/
		var c = new Components();
	}

	// }}}
	// {{{ public function new() : Void

	public function new() : Void
	{
		load();
	}

	// }}}
	// {{{ public function load() : Void

	public function load() : Void
	{
		var loader = new Loader();
		var me = this;
		loader.contentLoaderInfo.addEventListener( SecurityErrorEvent.SECURITY_ERROR, function(e) {
			trace("error: " + e);
		});
		loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, function(e) {
			trace("error: " + e);
		});
		loader.contentLoaderInfo.addEventListener( Event.COMPLETE, function(e){
			me.onComplete();
		});
		loader.contentLoaderInfo.addEventListener( Event.INIT, function(e){
		});
		RuntimeEngine.getStage().addChild(loader);

		loader.load(new URLRequest("res.swf"),new LoaderContext(false,ApplicationDomain.currentDomain));
		//var res = haxe.Resource.getBytes("btn");
		//loader.loadBytes(res.getData(), new LoaderContext(false,ApplicationDomain.currentDomain));
	}

	// }}}
	// {{{ public function onComplete() : Void

	public function onComplete() : Void
	{
		var me = this;
		// {{{ window
		var win = new UXWindow( new UXSize(500,300), "テスト・ウィンドウ 0001" );
		win.x = 100;
		win.y = 30;
		var tf = new UXTextField("",16,0x000000,new UXSize(200,20));
		tf.field.border = true;
		tf.field.borderColor = 0x000000;
		tf.x = 20;
		tf.y = 40;
		win.addUXChild( tf );
		UXRoot.addUXChild( win );
		// }}}
		// {{{ progress
		progress = new UXProgressBar(null,new UXSize(200,16));
		progress.x = 20;
		progress.y = 10;
		reverse = false;
		total = 100;
		//UXRoot.addUXChild( progress );
		win.addUXChild( progress );
		// }}}
		// {{{ button in win
		var btn0 = new UXButton(new UXLabel("ボタン",20));
		btn0.x = 100;
		btn0.y = 80;
		btn0.ctrl.getInteraction("Click").addAct( function(s):Void {
			me.count = me.reverse? me.total : 0;
			me.progress.startWatch( me.progressing );
		});
		//UXRoot.addUXChild( btn0 );
		win.addUXChild( btn0 );
		// }}}
		// {{{ button, label
		var btnres = UXComponentResourceFactory.create("MyButtonResource");
		var lbl = new UXLabel("aaa",16);
		lbl.x = 20;
		lbl.y = 140;
		//UXRoot.addUXChild( lbl );
		win.addUXChild( lbl );
		var btn1 = new UXButton("これは何でしょう？",btnres);
		btn1.x = 70;
		btn1.y = 140;
		//UXRoot.addUXChild( btn1 );
		win.addUXChild( btn1 );
		// }}}
		// {{{ selector
		var res1 = UXComponentResourceFactory.create("Male");
		var res2 = UXComponentResourceFactory.create("Female");
		var res3 = UXComponentResourceFactory.create("Male");
		var res4 = UXComponentResourceFactory.create("Female");
		var selector = new UXSelector(EXCLUSIVE);
		var item1 = new UXItem(res1,null,"男");
		var item2 = new UXItem(res2,null,"女");
		var item3 = new UXItem(res3,new UXLabel("少年"),"少年");
		var item4 = new UXItem(res4,new UXLabel("少女"),"少女");
		item3.state.resource.scaleX = 0.7;
		item3.state.resource.scaleY = 0.7;
		item4.state.resource.scaleX = 0.7;
		item4.state.resource.scaleY = 0.7;
		item1.x = 50;
		item1.y = 0;
		item2.x = 100;
		item2.y = 0;
		item3.x = 150;
		item3.y = 0;
		item4.x = 200;
		item4.y = 0;
		selector.addUXChild(item1);
		selector.addUXChild(item2);
		selector.addUXChild(item3);
		selector.addUXChild(item4);
		var cur:Int = 0;
		var inter1 = new UXKeyInteraction(Keyboard.LEFT);
		inter1.addAct( function(s:Dynamic){
			trace("left");
			cur--;
			if ( cur<0 ) cur = Std.int(s.items.length-1);
			s.eventTarget = s.items[cur];
		});
		inter1.bind("Select");
		var inter2 = new UXKeyInteraction(Keyboard.RIGHT);
		inter2.addAct( function(s:Dynamic){
			trace("right");
			cur++;
			if ( cur>s.items.length-1 ) cur = 0;
			s.eventTarget = s.items[cur];
		});
		inter2.bind("Select");
		selector.ctrl.addInteraction(inter1);
		selector.ctrl.addInteraction(inter2);
		selector.x = 20;
		selector.y = 190;

		//UXRoot.addUXChild( selector );
		win.addUXChild( selector );
		// }}}
		// {{{ scrollarea
		var scrollarea = new UXScrollArea(new UXSize(200,150));
		//UXRoot.addUXChild( scrollarea );
		win.addUXChild( scrollarea );
		var text = "01.あいうえおあいうえお　かきくけこ\n"
				+ "02.あいうえおあいうえお　かきくけこ\n"
				+ "03.あいうえおあいうえお　かきくけこ\n"
				+ "04.あいうえおあいうえお　かきくけこ\n"
				+ "05.あいうえおあいうえお　かきくけこ\n"
				+ "06.あいうえおあいうえお\n"
				+ "07.あいうえおあいうえお\n"
				+ "08.あいうえおあいうえお\n"
				+ "09.あいうえおあいうえお　かきくけこ\n"
				+ "10.あいうえおあいうえお　かきくけこ\n"
				+ "11.あいうえおあいうえお\n"
				+ "12.あいうえおあいうえお\n"
				;
		var t = new UXLabel(text,16);
		//t.label.autoSize = flash.text.TextFieldAutoSize.NONE;
		t.label.width = 300;
		t.label.selectable = true;
		t.label.wordWrap = true;
		t.label.mouseWheelEnabled = true;
		scrollarea.addUXChild( t );
		trace(t.label.textHeight);
		//var com:Dynamic = new jp.ize.ux.components.UXBasicComponent();
		//scrollarea.addUXChild( com );
		//trace( com.width );
		//trace( com.height );
		scrollarea.x = 50;
		scrollarea.y = 310;
		// }}}
		// {{{ image
		flash.system.Security.allowInsecureDomain("*");
		var image = new UXImage("http://www.openplatform.jp/ize/webplatform/img/icons/ax-applet.png",null,false);
		image.x = 0;
		image.y = 0;
		image.onComplete = function() { trace("complete"); }
		image.onError = function(e) { trace(e); }
		image.load();
		//UXRoot.addUXChild( image );
		win.addUXChild( image );
		trace("image load.");
		// }}}
		// {{{ slider
		var slider = new UXSlider(new UXSize(20,50),UXSliderDirection.VERTICAL);
		slider.x = 200;
		slider.y = 120;
		win.addUXChild( slider );
		// }}}
		// {{{ slider
		var slider = new UXSlider(new UXSize(100,16),UXSliderDirection.HORIZONTAL);
		slider.x = 250;
		slider.y = 140;
		win.addUXChild( slider );
		// }}}
		// {{{ button in win
		var btn = new UXButton(new UXLabel("ウィンドウを開く",20));
		btn.x = 20;
		btn.y = 20;
		var modal = true;
		btn.ctrl.getInteraction("Click").addAct( function(s):Void {
			Act.exec( win.state.acts.show(), win.state );
			win.top();
			win.state.modal = modal;
			win.state.closeButton = modal;
			win.state.resizable = modal;
			modal = !modal;
		});
		UXRoot.addUXChild( btn );
		// }}}
		win.top();
	}

	// }}}
	// {{{ public function progressing() : Float

	public function progressing() : Float
	{
		if ( reverse ) {
			count--;
		} else {
			count++;
		}
		if ( (!reverse&&count==total) || (reverse&&count==0) ) {
			progress.stopWatch();
			reverse = !reverse;
		}
		return count/total;
	}

	// }}}
}
